/*
Kiwi Editor is a framework for building WYSIWYG web editors.
Copyright (C) 2011  Roosevelt de Lima Sardinha

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

Contact the author at roosevelt1@gmail.com (Roosevelt) 
*/

function table (tableId){
	
	var thisObject = this;
	this.table = null;
	
	if (tableId == null)
	{
		document.write("Error: table: tableId cannot be null.");	
		return;
	}

	
	// Check if the table already exist, if it doesn't, creates it.	
	this.table = document.getElementById(tableId);
	if (this.table == null)
	{
		var table = document.createElement('table');
		table.id = tableId;
		this.table = table;
	}
	
	this.mergeCells = function(row, cell, rowspan, colspan)
	{
		//Merges several cells based on attribute values.
		//row indicates the row of the first cell to be merged (first is the upper left cell in the set of ones you want to merge)
		//cell indicates the cell of the row of the first cell to be merged (first is the upper left cell in the set of ones you want to merge)
		//rowspan indicates how much cells you want to horizontally merge.
		//colspan indicates how much collumns or cells you want to vertically merge.

		if (row ==  null) 
		{
			row = 0;
		}
		
		if (cell == null)
		{
			cell = 0;
		}
		
		if (rowspan == null)
		{
			rowspan = 1;
		}
		
		if (colspan == null)
		{
			colspan = 1;
		}
		
		//remove merged cells 
		for (var i=0; i < colspan; i++)
		{
			if (i!=0)
			{
				thisObject.table.rows[row].deleteCell(cell);	
			}
			for (var j=1; j < rowspan; j++)
			{
				thisObject.table.rows[row + j].deleteCell(cell);
			}
		}	

		//the first upper left cell
		var mergeCell = thisObject.table.rows[row].cells[cell];

		//changes cell colspan and rowspan		
		mergeCell.colSpan = colspan;
		mergeCell.rowSpan = rowspan;
	}

	this.maxCellsLength = function(){
		//This method returns the length of the row with more cells in the table.
		maxCellsLength = 0;
		for (var i = 0; i < thisObject.table.rows.length; i++)
		{
			if(maxCellsLength < thisObject.table.rows[i].cells.length)
			{
				maxCellsLength = thisObject.table.rows[i].cells.length;
			}		
		}
		return maxCellsLength;
	}

	this.insertElement = function(row, cell, rowspan, colspan, content){
		/*Inserts an element in a table, if the cell into which you want to insert the element doesn't exist, the table is extended with more rows and cells.
		#row indicates the number of the row
		#cell indicates the cell in the row that is going to receive the content
		#colspan indicates the value of the colspan property of this cell
		#rowspan indicates the value of the rowspan property of this cell
		#content is the DOM element to be added to the cell*/
		
		//validating arguments
		if (row == null)
		{
			row = 0;
		}

		if (cell == null)
		{
			cell = 0;
		}

		if (colspan == null)
		{
			colspan = 1;
		}
	
		if (rowspan == null)
		{
			rowspan = 1;
		}

		if (content == null)
		{
			content = document.write('Error: table -> insertElement: content cannot be null.');
			return;
		}

		if(((row+rowspan) > thisObject.table.rows.length) || ((cell + colspan) > thisObject.maxCellsLength()))
		{
			var rowCount;
			if ((row + rowspan) > thisObject.table.rows.length)
			{
				rowCount = row + rowspan; 
			}
			else
			{
				rowCount = thisObject.table.rows.length;
			}

			var cellCount;
			if ((cell + colspan) > thisObject.maxCellsLength())
			{
				cellCount = cell + colspan; 
			}
			else
			{
				cellCount = thisObject.maxCellsLength();
			}
			//creating table cells
			for(var i=0; i < rowCount; i++)
			{	
				if (thisObject.table.rows[i] == undefined)
				{
					thisObject.table.insertRow(-1);
				}

				for(var j=0; j < cellCount; j++)
				{
					if (thisObject.table.rows[i].cells[j] == undefined)
					{
						thisObject.table.rows[i].insertCell(-1);
					}
				}
			}
		}

		if (!((rowspan ==1 ) && (colspan == 1)))
		{
			thisObject.mergeCells(row, cell, rowspan, colspan);
		}

		//everything is ok, then we can insert the content into the cell.
		thisObject.table.rows[row].cells[cell].appendChild(content);	
	}
}
